//
//  MM_CodeResultListVC.m
//  MallManage_IOS
//
//  Created by 张浩 on 2023/10/16.
//

#import "MM_CodeResultListVC.h"
#import "MM_CodeResultSectionModel.h"
#import "MM_CodeResultInfoCell.h"
#import "MM_CodeResultInfoModel.h"
#import "MM_CodeResultStorageRackCell.h"
#import "MM_CodeResultAlertView.h"
#import "MM_CodeResultTakeStockInfoModel.h"
#import "MM_CodeResultOperationRecordCell.h"
#import "MM_HomeSkuDdetailGiveawayListVC.h"
#import "MM_CodeResultListPicCell.h"
#import "TZImagePickerController.h"
#import "MM_CodeResultPicModel.h"
#import "MM_CodeResultIncreaseDecreaseAlertView.h"
#import "MM_AlertView.h"
#import "MM_CodeResultPicView.h"

@interface MM_CodeResultListVC ()<UITableViewDelegate,UITableViewDataSource,TZImagePickerControllerDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) NSMutableArray *tempArr;
@property (nonatomic, strong) MM_CodeResultInfoModel *userInfoModel;
@property (nonatomic, strong) UIView *bottomView;
@property (nonatomic, strong) MM_CodeResultAlertView *resultAlertView;
@property (nonatomic, copy) NSString *stockCount_id;
@property (nonatomic, assign) NSInteger page_num;
@property (nonatomic, strong) MM_CodeResultIncreaseDecreaseAlertView *incrDecrAlertView;
@property (nonatomic, strong) NSMutableArray *resonArr;
@property (nonatomic, copy) NSString *goods_location_id;
@property (nonatomic, copy) NSString *job_type;
@property (nonatomic, strong) MM_CodeResultPicView *picAlertView;

@end

@implementation MM_CodeResultListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleView.title = @"规格信息";
    self.titleView.titleType = MM_TitleTypeRight;
    [self.titleView.rightBtn setTitle:@"查看赠品" forState:UIControlStateNormal];
    [self.titleView.rightBtn setTitleColor:MM_ColorUtil(@"1F71C4") forState:UIControlStateNormal];
    
    [self.view addSubview:self.tableView];
    self.page_num = 1;
    [self setupRefresh];
    
    [self showHudInView:self.view hint:@"正在加载数据..."];
    [self loadData];
    [self loadNumberResonData];
}

- (void)MM_TitleView:(MM_TitleView *)titleView clickBackButton:(UIButton *)backbtn {
    
    UIViewController *backVC = [self backToVCStr:@"MM_HomeManageVC"];
    
    if (backVC) {
        [self.navigationController popToViewController:backVC animated:YES];
    } else {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)MM_TitleView:(MM_TitleView *)titleView clickRightButton:(UIButton *)rightbtn {
    
    if ([self.userInfoModel.have_gift boolValue]) {
        
        MM_HomeSkuDdetailGiveawayListVC *listVC = [[MM_HomeSkuDdetailGiveawayListVC alloc] init];
        listVC.sku_id = self.userInfoModel.sku_id;
        [self.navigationController pushViewController:listVC animated:YES];
    } else {
        [self showStateText:@"该规格暂无赠品!"];
    }
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.tempArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    MM_CodeResultSectionModel *sectionModel = self.tempArr[section];
    return sectionModel.sectionArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MM_CodeResultSectionModel *sectionModel = self.tempArr[indexPath.section];
    
    if (sectionModel.sectionType == MM_CodeResultSectionTypeInfo || sectionModel.sectionType == MM_CodeResultSectionTypeInfoDesc) {
        
        MM_CodeResultInfoCell *cell = [MM_CodeResultInfoCell cellWithTableView:tableView];
        cell.model = sectionModel.sectionArr[indexPath.row]; 
        WS(weakSelf);
        cell.changeBlock = ^(MM_CodeResultListModel * _Nonnull model, NSInteger index) {
          
            if (index == 0) {
                if ([model.nameStr isEqualToString:@"旧编码"]) {
                    [weakSelf oldCodeData];
                } else if ([model.nameStr isEqualToString:@"控价"]) {
                    [weakSelf oldPriceData];
                }
            } else {
                
                if ([model.nameStr isEqualToString:@"商城价"]) {
                    
                    [MM_AlertView MM_AlertWithTitle:@"" message:@"抖音、小红书、视频号、天猫等平台的商品卡展示价格，有库存的时候客户可直接用该价格下单。" withJH_AlertBlock:^(MM_AlertViewBtnType buttonIndexType) {
                    } cancelButtonTitle:nil ensureButtonTitle:@"确定"];
                } else if ([model.nameStr isEqualToString:@"控价"]) {
                    
                    [MM_AlertView MM_AlertWithTitle:@"" message:@"直播间或者其他场合促销时商品销售的最低价格，如未设置该价格则控价等于商城价" withJH_AlertBlock:^(MM_AlertViewBtnType buttonIndexType) {
                    } cancelButtonTitle:nil ensureButtonTitle:@"确定"];
                }
                
            }
        };
        
        return cell;
        
    } else if (sectionModel.sectionType == MM_CodeResultSectionTypeReservoirArea) {
        
        MM_CodeResultStorageRackCell *cell = [MM_CodeResultStorageRackCell cellWithTableView:tableView];
        cell.model = sectionModel.sectionArr[indexPath.row];
        WS(weakSelf);
        cell.changeBlock = ^(MM_CodeResultSkuLocationModel * _Nonnull model) {
            
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
            UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"增加库存" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                weakSelf.incrDecrAlertView.titleStr = @"增加库存";
                
                NSMutableArray *tempArr = [NSMutableArray array];
                
                for (MM_CodeResultResonModel *model in weakSelf.resonArr) {
                    
                    if (model.dict_type.integerValue == 2) {
                        
                        if (!stringIsEmpty(model.dict_value)) {
                            [tempArr addObject:model];
                        }
                    }
                }
                weakSelf.job_type = @"5";
                weakSelf.goods_location_id = model.goods_location_id;
                weakSelf.incrDecrAlertView.resonArr = tempArr;
                [weakSelf.incrDecrAlertView show];
//                [weakSelf codeStr:@"5" andGoods_location_id:model.goods_location_id];
            }];
            UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"减少库存" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                weakSelf.incrDecrAlertView.titleStr = @"减少库存";
                
                NSMutableArray *tempArr = [NSMutableArray array];
                
                for (MM_CodeResultResonModel *model in weakSelf.resonArr) {
                    
                    if (model.dict_type.integerValue == 1) {
                        
                        if (!stringIsEmpty(model.dict_value)) {
                            [tempArr addObject:model];
                        }
                    }
                }
                weakSelf.incrDecrAlertView.resonArr = tempArr;
                [weakSelf.incrDecrAlertView show];
                weakSelf.job_type = @"6";
                weakSelf.goods_location_id = model.goods_location_id;
//                [weakSelf codeStr:@"6" andGoods_location_id:model.goods_location_id];
            }];
            
            UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"库存盘点" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                [weakSelf stockCount_1Data:model];
            }];
            
            UIAlertAction *action4 = [UIAlertAction actionWithTitle:@"商品下架" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                
                [weakSelf loadDelistData:model];
            }];
            
            UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
                
            }];
            [action1 setValue:MM_ColorUtil(@"#1F71C4") forKey:@"titleTextColor"];
            [action2 setValue:MM_ColorUtil(@"#1F71C4") forKey:@"titleTextColor"];
            [action3 setValue:MM_ColorUtil(@"#DE1212") forKey:@"titleTextColor"];
            [action4 setValue:MM_ColorUtil(@"#1F71C4") forKey:@"titleTextColor"];
            [cancelAction setValue:MM_ColorUtil(@"#1F71C4") forKey:@"titleTextColor"];
            [alert addAction:action1];
            [alert addAction:action2];
            [alert addAction:action3];
            [alert addAction:action4];
            [alert addAction:cancelAction];
            [self presentViewController:alert animated:YES completion:nil];

        };
        
        return cell;
    } else if (sectionModel.sectionType == MM_CodeResultSectionTypePic) {

        MM_CodeResultListPicCell *cell = [MM_CodeResultListPicCell cellWithTableView:tableView];
        cell.model = sectionModel.sectionArr[indexPath.row];
        WS(weakSelf);
        cell.addPicBlock = ^{
            
            TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc]initWithMaxImagesCount:6 delegate:weakSelf];
            imagePickerVc.allowPickingGif = NO;
            imagePickerVc.allowPickingOriginalPhoto = YES;
            imagePickerVc.allowPickingMultipleVideo = NO;
            imagePickerVc.allowTakeVideo = NO;
            imagePickerVc.allowTakePicture = YES;
            imagePickerVc.barItemTextColor = [ColorUtil colorWithHexadecimal:@"#333333"];
            if (@available(iOS 11.0, *)) {
                imagePickerVc.modalPresentationStyle = UIModalPresentationOverCurrentContext;
            } else {
            }
            [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
                
                NSMutableArray *imageArr = [NSMutableArray array];
                
                for (int i=0; i<photos.count; i++) {
                    
                    UIImage *image = photos[i];
                    PHAsset *asset = assets[i];

                    NSString *fileName = [asset valueForKey:@"filename"];
                    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                    NSString *filePath = [[paths objectAtIndex:0]stringByAppendingPathComponent:fileName];  // 保存文件的名称
                    BOOL result = [UIImagePNGRepresentation(image)writeToFile:filePath atomically:YES];
                        
                    if (result) {
                        
                        MM_UploadImageModel *imageModel = [[MM_UploadImageModel alloc] init];
                        imageModel.canUploadImage = YES;
                        imageModel.tempImage = image;
                        imageModel.file_path = filePath;
                        imageModel.sku_id = weakSelf.userInfoModel.sku_id;
                        [imageArr addObject:imageModel];
                    }
                }
                
                MM_CodeResultListPicModel *picModel = sectionModel.sectionArr.firstObject;
                
                if (imageArr.count > 0) {
                    [picModel.imageArr addObjectsFrom_MyArr:imageArr];
                }
                
                //主线程刷新ui
                dispatch_async(dispatch_get_main_queue(), ^(){
                    
                    [weakSelf.tableView reloadData];
                });
            }];
                            
            [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:imagePickerVc animated:YES completion:nil];
        };
        
        cell.deleteBlock = ^(MM_UploadImageModel * _Nonnull imageModel) {
            
            NSMutableArray *tempArr = [NSMutableArray array];
            
            if (imageModel.picParmDic.count > 0) {
                [tempArr addObject:imageModel.picParmDic];
            }
            NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
            parmDic[@"sku_id"] = weakSelf.userInfoModel.sku_id;
            parmDic[@"skuFiles"] = tempArr;
            
            [MM_Http deleteRequestURLStr:KMM_Spu_Files withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
                
                [weakSelf showStateText:@"删除成功!"];
                
                //主线程刷新ui
                dispatch_async(dispatch_get_main_queue(), ^(){
                    
                    [weakSelf.tableView reloadData];
                });
                
            } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
            }];
            
        };
        
        return cell;
    }else if (sectionModel.sectionType == MM_CodeResultSectionTypeOperationRecord) {
        
        MM_CodeResultOperationRecordCell *cell = [MM_CodeResultOperationRecordCell cellWithTableView:tableView];
        cell.model = sectionModel.sectionArr[indexPath.row];
        WS(weakSelf);
        cell.remarkBlock = ^(MM_CodeResultOperationRecordInfoModel * _Nonnull model) {
            
            if (!stringIsEmpty(model.reason) || model.files.count > 0) {
                [weakSelf.picAlertView setUpResonStr:model.reason andImageArr:model.files];
                [weakSelf.picAlertView show];
            }
        };
        
        return cell;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MM_CodeResultSectionModel *sectionModel = self.tempArr[indexPath.section];

    if (sectionModel.sectionType == MM_CodeResultSectionTypePic) {
        
        MM_CodeResultListPicModel *picModel = sectionModel.sectionArr[indexPath.row];
        return picModel.cellHeight;
        
    } else {
        return UITableViewAutomaticDimension;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    MM_CodeResultSectionModel *sectionModel = self.tempArr[section];
    
    if (sectionModel.sectionType == MM_CodeResultSectionTypeInfoDesc || sectionModel.sectionType == MM_CodeResultSectionTypeReservoirArea) {
        return 10;
    }
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    MM_CodeResultSectionModel *sectionModel = self.tempArr[section];
    
    if (sectionModel.sectionType == MM_CodeResultSectionTypeReservoirArea) {
        
        NSArray *titleArr = @[@"库区",@"库位编码",@"货架号",@"层号",@"位号",@"库存",@"操作"];
        CGFloat titleW = ScreenW /7.0;
        
        UIView *hederView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 40)];
        hederView.backgroundColor = [UIColor whiteColor];
        
        for (UIView*subView in hederView.subviews) {
            [subView removeFromSuperview];
        }
        
        for (int i=0; i<titleArr.count; i++) {
            
            UIView *titleView = [[UIView alloc] init];
            titleView.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
            [hederView addSubview:titleView];
            
            [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
               
                make.left.equalTo(titleW *i);
                make.top.equalTo(hederView);
                make.width.equalTo(titleW);
                make.height.equalTo(40);
            }];
                        
            UILabel *nameLabel = [Custom_ZHTool uiLabelCreate:titleArr[i] Font:FONT(13) TextColor:[ColorUtil colorWithHexadecimal:@"#333333"]];
            nameLabel.text = titleArr[i];
            nameLabel.font = FONT_BOLD(12);
            nameLabel.textAlignment = NSTextAlignmentCenter;
            [titleView addSubview:nameLabel];
            
            [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
//                if (i == 0) {
//                    make.left.equalTo(12);
//                }else {
//                    make.left.equalTo(10);
//                }
//
                make.centerX.centerY.equalTo(titleView);
            }];
            
//            UIView *lineView = [[UIView alloc] init];
//            lineView.backgroundColor = [ColorUtil colorWithHexadecimal:@"#DDDDDD"];
//            [titleView addSubview:lineView];
//
//            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//                make.right.top.equalTo(titleView);
//    //            make.top.equalTo(self.scrollView);
//                make.width.equalTo(0.5);
//                make.height.equalTo(40);
//            }];

        }

        return hederView;

    }else if (sectionModel.sectionType == MM_CodeResultSectionTypeOperationRecord) {
        
        NSArray *titleArr = @[@"操作类型",@"库位信息",@"差异数量\n剩余库存",@"备注",@"操作人\n操作时间",];
        CGFloat titleW = ScreenW /5.0;
        
        UIView *hederView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 60)];
        hederView.backgroundColor = [UIColor whiteColor];
        
        for (UIView*subView in hederView.subviews) {
            [subView removeFromSuperview];
        }
        
        UILabel *nameLabel = [Custom_ZHTool uiLabelCreate:@"操作记录" Font:FONT_BOLD(14) TextColor:MM_ColorUtil(@"#333333")];
        [hederView addSubview:nameLabel];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.left.equalTo(14);
            make.top.equalTo(5);
            make.right.equalTo(-12);
        }];
        
        for (int i=0; i<titleArr.count; i++) {
            
            UIView *titleView = [[UIView alloc] init];
            titleView.backgroundColor = [ColorUtil colorWithHexadecimal:@"#ffffff"];
            [hederView addSubview:titleView];
            
            [titleView mas_makeConstraints:^(MASConstraintMaker *make) {
               
                make.left.equalTo(titleW *i);
                make.top.equalTo(nameLabel.mas_bottom).offset(5);
                make.width.equalTo(titleW);
                make.height.equalTo(40);
            }];
                        
            UILabel *nameLabel = [Custom_ZHTool uiLabelCreate:titleArr[i] Font:FONT(13) TextColor:[ColorUtil colorWithHexadecimal:@"#333333"]];
            nameLabel.text = titleArr[i];
            nameLabel.font = FONT_BOLD(12);
            nameLabel.numberOfLines = 2;
            nameLabel.textAlignment = NSTextAlignmentCenter;
            [titleView addSubview:nameLabel];
            
            [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                
//                if (i == 0) {
//                    make.left.equalTo(12);
//                }else {
//                    make.left.equalTo(10);
//                }
//
                make.centerX.centerY.equalTo(titleView);
            }];
            
//            UIView *lineView = [[UIView alloc] init];
//            lineView.backgroundColor = [ColorUtil colorWithHexadecimal:@"#DDDDDD"];
//            [titleView addSubview:lineView];
//
//            [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
//
//                make.right.top.equalTo(titleView);
//    //            make.top.equalTo(self.scrollView);
//                make.width.equalTo(0.5);
//                make.height.equalTo(40);
//            }];

        }

        return hederView;

    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    MM_CodeResultSectionModel *sectionModel = self.tempArr[section];
    
    if (sectionModel.sectionType == MM_CodeResultSectionTypeInfoDesc) {
        return 10;
    }else if (sectionModel.sectionType == MM_CodeResultSectionTypeReservoirArea) {
        
        return 40;
    }else if (sectionModel.sectionType == MM_CodeResultSectionTypeOperationRecord) {
        
        return 60;
    }
    return 0.01;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}

- (void)loadTempData {
    
    [self.dataArr removeAllObjects];
    
    MM_CodeResultSectionModel *infoSectionModel = [[MM_CodeResultSectionModel alloc] init];
    infoSectionModel.sectionType = MM_CodeResultSectionTypeInfo;
    [self.dataArr addObject:infoSectionModel];

    MM_CodeResultSectionModel *descSectionModel = [[MM_CodeResultSectionModel alloc] init];
    descSectionModel.sectionType = MM_CodeResultSectionTypeInfoDesc;
    [self.dataArr addObject:descSectionModel];

    MM_CodeResultSectionModel *aeservoirAreaSectionModel = [[MM_CodeResultSectionModel alloc] init];
    aeservoirAreaSectionModel.sectionType = MM_CodeResultSectionTypeReservoirArea;
    [self.dataArr addObject:aeservoirAreaSectionModel];

    MM_CodeResultSectionModel *picSectionModel = [[MM_CodeResultSectionModel alloc] init];
    picSectionModel.sectionType = MM_CodeResultSectionTypePic;
    [self.dataArr addObject:picSectionModel];

    MM_CodeResultSectionModel *recordSectionModel = [[MM_CodeResultSectionModel alloc] init];
    recordSectionModel.sectionType = MM_CodeResultSectionTypeOperationRecord;
    [self.dataArr addObject:recordSectionModel];
    
}

- (void)loadData {
    
    [self loadTempData];
        
    dispatch_group_t dispatchGroup = dispatch_group_create();//创建信号组
    
    dispatch_group_enter(dispatchGroup);
    
    [self loadCodeData:^{
        //移除信号
        dispatch_group_leave(dispatchGroup);
    }];
    
    dispatch_group_enter(dispatchGroup);
    
    [self loadOperationRecordData:^{
        //移除信号
        dispatch_group_leave(dispatchGroup);
    }];
    
    WS(weakSelf);
    dispatch_group_notify(dispatchGroup,dispatch_get_main_queue(),^(){
        
        [weakSelf endRefersh];
        [weakSelf updateTmpArr];
//        [weakSelf.tableView reloadData];
        [weakSelf hideHud];
        [weakSelf.tableView reloadData];
     });
 }

- (void)loadCodeData:(void(^)(void))success {
    
    WS(weakSelf);
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    parmDic[@"sku_code"] = self.code_str;
    [MM_Http getRequestURLStr:KMM_Spu_SpuSku withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
//        [self endRefersh];
        
        weakSelf.userInfoModel = [MM_CodeResultInfoModel mj_objectWithKeyValues:requestDic];
        
        NSArray *nameArr_1 = @[@"品牌名称",@"商品名称",@"商品类别",@"商品条码"];
        NSArray *descArr_1 = @[!stringIsEmpty(weakSelf.userInfoModel.supplier_name)?weakSelf.userInfoModel.supplier_name:@"",!stringIsEmpty(weakSelf.userInfoModel.spu_name)?weakSelf.userInfoModel.spu_name:@"",!stringIsEmpty(weakSelf.userInfoModel.category_name)?weakSelf.userInfoModel.category_name:@"",!stringIsEmpty(weakSelf.userInfoModel.spu_code)?weakSelf.userInfoModel.spu_code:@""];
                
        MM_CodeResultSectionModel *sectionModel_1 = [weakSelf sectionModelWithType:MM_CodeResultSectionTypeInfo];
        [sectionModel_1.sectionArr removeAllObjects];
        
        for (int i=0; i<nameArr_1.count; i++) {
            
            MM_CodeResultListModel *model = [[MM_CodeResultListModel alloc] init];
            model.nameStr = nameArr_1[i];
            model.descStr = descArr_1[i];
            [sectionModel_1.sectionArr addObject:model];
        }
        
        NSArray *nameArr_2 = @[@"SKUID",@"规格名称",@"规格编码",@"旧编码",@"规格单位",@"商城价",@"控价",@"预警数量",@"可用库存"];
        NSArray *descArr_2 = @[!stringIsEmpty(weakSelf.userInfoModel.sku_id)?weakSelf.userInfoModel.sku_id
                                :@"",!stringIsEmpty(weakSelf.userInfoModel.sku_name)?weakSelf.userInfoModel.sku_name:@"",!stringIsEmpty(weakSelf.userInfoModel.sku_code)?weakSelf.userInfoModel.sku_code:@"",!stringIsEmpty(weakSelf.userInfoModel.old_code)?weakSelf.userInfoModel.old_code:@"",!stringIsEmpty(weakSelf.userInfoModel.unit)?weakSelf.userInfoModel.unit:@"",!stringIsEmpty(weakSelf.userInfoModel.reference_price)?[NSString stringWithFormat:@"%@元",weakSelf.userInfoModel.reference_price]:@"",!stringIsEmpty(weakSelf.userInfoModel.price_control)?[NSString stringWithFormat:@"%@元",[Custom_ZHTool precisionLossNumber:weakSelf.userInfoModel.price_control]]:@"",weakSelf.userInfoModel.alert_count.integerValue<0?@"不限":weakSelf.userInfoModel.alert_count,!stringIsEmpty(weakSelf.userInfoModel.available_qty)?weakSelf.userInfoModel.available_qty:@""];
        
        MM_CodeResultSectionModel *sectionModel_2 = [weakSelf sectionModelWithType:MM_CodeResultSectionTypeInfoDesc];
        [sectionModel_2.sectionArr removeAllObjects];
        
        for (int i=0; i<nameArr_2.count; i++) {
            
            MM_CodeResultListModel *model = [[MM_CodeResultListModel alloc] init];
            model.nameStr = nameArr_2[i];
            model.descStr = descArr_2[i];
            [sectionModel_2.sectionArr addObject:model];
        }
        
        MM_CodeResultSectionModel *sectionModel_3 = [weakSelf sectionModelWithType:MM_CodeResultSectionTypeReservoirArea];
        [sectionModel_3.sectionArr removeAllObjects];
        
        if (weakSelf.userInfoModel.skuLocation.count > 0) {
            [sectionModel_3.sectionArr addObjectsFrom_MyArr:weakSelf.userInfoModel.skuLocation];
        }
        [weakSelf updateTmpArr];
        
        [weakSelf loadSkuPicData:^{
            
            if (success) {
                success();
            }
        }];
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        if (success) {
            success();
        }
//        [self endRefersh];
        [self hideHud];
        [self showStateText:errorInfo];
    }];
}

- (void)loadSkuPicData:(void(^)(void))success {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    [MM_Http getRequestURLStr:[NSString stringWithFormat:@"%@/%@",KMM_Spu_Files_id,self.userInfoModel.sku_id] withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
//        [self endRefersh];
        
        NSArray *dataArr = [MM_CodeResultPicModel mj_objectArrayWithKeyValuesArray:requestDic[@"data"]];
        
        if (dataArr.count > 0) {
            
            MM_CodeResultSectionModel *picSectionModel = [self sectionModelWithType:MM_CodeResultSectionTypePic];
            [picSectionModel.sectionArr removeAllObjects];
            MM_CodeResultListPicModel *picInfoModel = [[MM_CodeResultListPicModel alloc] init];

            for (int i=0; i<dataArr.count; i++) {
                
                MM_CodeResultPicModel *model = dataArr[i];
                
                MM_UploadImageModel *imageModel = [[MM_UploadImageModel alloc] init];
                imageModel.canUploadImage = NO;
                imageModel.img = model.url;
                imageModel.picParmDic = requestDic[@"data"][i];
    //            imageModel.file_path = filePath;
                imageModel.sku_id = self.userInfoModel.sku_id;
                [picInfoModel.imageArr addObject:imageModel];
            }
//            for (MM_CodeResultPicModel *model in dataArr) {
//                
//            }
            [picSectionModel.sectionArr addObject:picInfoModel];
            
        } else {
            MM_CodeResultSectionModel *picSectionModel = [self sectionModelWithType:MM_CodeResultSectionTypePic];
            [picSectionModel.sectionArr removeAllObjects];
            MM_CodeResultListPicModel *picInfoModel = [[MM_CodeResultListPicModel alloc] init];
            [picSectionModel.sectionArr addObject:picInfoModel];
            
        }
        
        if (success) {
            success();
        }
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        if (success) {
            success();
        }
//        [self endRefersh];
        [self hideHud];
        [self showStateText:errorInfo];
    }];
}

- (void)loadOperationRecordData:(void(^)(void))success {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    parmDic[@"sku_code"] = self.code_str;
    parmDic[@"pageIndex"] = @(self.page_num).description;
    parmDic[@"pagesize"] = @"10";
//    change_options reason after_change_qty
    [self showHudInView:self.view hint:@"正在加载数据..."];
    [MM_Http postRequestURLStr:KMM_Stock_OperationRecordsList withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        [self endRefersh];
                
        NSString *total_count = [requestDic[@"totals"] description];

        NSArray *dataArr = [MM_CodeResultOperationRecordInfoModel mj_objectArrayWithKeyValuesArray:requestDic[@"rows"]];
        
        if (self.page_num <= 1) {
             
            MM_CodeResultSectionModel *sectionModel = [self sectionModelWithType:MM_CodeResultSectionTypeOperationRecord];
            [sectionModel.sectionArr removeAllObjects];
                        
            if (dataArr.count > 0) {
                [sectionModel.sectionArr addObjectsFrom_MyArr:dataArr];
            }else {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        
        MM_CodeResultSectionModel *resultSectionModel = [self sectionModelWithType:MM_CodeResultSectionTypeOperationRecord];
        
        if (total_count.integerValue == resultSectionModel.sectionArr.count && resultSectionModel.sectionArr.count > 0) {
            
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        }else {
            
            if (total_count.integerValue > 0) {
                [self.tableView.mj_footer endRefreshing];
            } else {
                self.tableView.mj_footer = nil;
            }
            
        }
        [self updateTmpArr];
                
        if (success) {
            success();
        }
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        if (success) {
            success();
        }
        [self endRefersh];
        [self hideHud];
        [self showStateText:errorInfo];
    }];
}

- (void)loadOperationRecordMoreData {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    parmDic[@"sku_code"] = self.code_str;
    parmDic[@"pageIndex"] = @(self.page_num).description;
    parmDic[@"pagesize"] = @"10";

    [self showHudInView:self.view hint:@"正在加载数据..."];
    [MM_Http postRequestURLStr:KMM_Stock_OperationRecordsList withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        [self hideHud];
        [self endRefersh];
        
        NSString *total_count = [requestDic[@"totals"] description];
        
        NSArray *dataArr = [MM_CodeResultOperationRecordInfoModel mj_objectArrayWithKeyValuesArray:requestDic[@"rows"]];
        
        if (self.page_num <= 1) {
            
            MM_CodeResultSectionModel *sectionModel = [self sectionModelWithType:MM_CodeResultSectionTypeOperationRecord];
            [sectionModel.sectionArr removeAllObjects];
                    
        } else {
            
            MM_CodeResultSectionModel *sectionModel = [self sectionModelWithType:MM_CodeResultSectionTypeOperationRecord];
            [sectionModel.sectionArr addObjectsFrom_MyArr:dataArr];
            
        }
        
        [self updateTmpArr];
        [self.tableView reloadData];
        
        MM_CodeResultSectionModel *resultSectionModel = [self sectionModelWithType:MM_CodeResultSectionTypeOperationRecord];

        if (total_count.integerValue == resultSectionModel.sectionArr.count && resultSectionModel.sectionArr.count > 0) {
            
            if (resultSectionModel.sectionArr.count > 10) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }else {
            [self.tableView.mj_footer endRefreshing];
        }
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        self.page_num -- ;
        
        if (self.page_num <= 1) {
            self.page_num = 1;
        }
        
        [self endRefersh];
        [self hideHud];
        [self showStateText:errorInfo];
    }];
}

- (void)loadChangeNumberData:(NSMutableDictionary *)parmDic {
        
    parmDic[@"sku_id"] = self.userInfoModel.sku_id;
    
    if (!stringIsEmpty(self.goods_location_id)) {
        
//        MM_CodeResultSkuLocationModel *model = self.userInfoModel.skuLocation.firstObject;
        parmDic[@"goods_location_id"] = self.goods_location_id;

    }
    parmDic[@"job_type"] = self.job_type;
    
    [self showHudInView:self.view hint:@"正在加载数据..."];
    [MM_Http postRequestURLStr:KMM_Stock_StockChange withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        [self hideHud];
        
        [self showStateText:@"设置成功"];
        [self refershData];
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        [self hideHud];
        [self showStateText:errorInfo];
    }];
}

- (void)loadOldCodeData:(NSMutableDictionary *)parmDic {
            
    [self showHudInView:self.view hint:@"正在加载数据..."];
    [MM_Http putRequestURLStr:KMM_Spu_Part withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        [self hideHud];
        
        [self showStateText:@"更新成功"];
        [self refershData];
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        [self hideHud];
        [self showStateText:errorInfo];
    }];
}

- (void)loadNumberResonData {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    
    [self showHudInView:self.view hint:@"正在加载数据..."];
    [MM_Http getRequestURLStr:KMM_Sysdict_ChangeOptions withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        [self hideHud];
        
        [self.resonArr removeAllObjects];
        
        NSArray *dataArr = [MM_CodeResultResonModel mj_objectArrayWithKeyValuesArray:requestDic[@"data"]];
        
        [self.resonArr addObjectsFrom_MyArr:dataArr];
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        [self hideHud];
        [self showStateText:errorInfo];
    }];
}

- (void)refershData {
    
    self.page_num = 1;
    [self loadData];
}

- (void)loadDelistData:(MM_CodeResultSkuLocationModel *)model {
    
    if (model.available_qty.integerValue > 0) {
        [self showStateText:@"下架需把库存清零!"];
        return;
    }
    
    WS(weakSelf);
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"您确定要下架吗?" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *defaultAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        [weakSelf loadDelistCodeData:model];
    }];
    [defaultAction setValue:MM_ColorUtil(@"#999999") forKey:@"titleTextColor"];
    [cancelAction setValue:MM_ColorUtil(@"#1F71C4") forKey:@"titleTextColor"];
    [alert addAction:defaultAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
    
}

- (void)loadDelistCodeData:(MM_CodeResultSkuLocationModel *)model {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    parmDic[@"sku_id"] = self.userInfoModel.sku_id;
    parmDic[@"goods_location_id"] = model.goods_location_id;
    
    [self showHudInView:self.view hint:@"正在加载数据..."];
    [MM_Http postRequestURLStr:KMM_Stock_OffShelf withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        [self hideHud];
        
        [self showStateText:@"下架成功"];
        
        [self refershData];
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        [self hideHud];
        [self showStateText:errorInfo];
    }];

}

//库存盘点第一步
- (void)stockCount_1Data:(MM_CodeResultSkuLocationModel *)model {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    parmDic[@"id"] = @"0";
    parmDic[@"job_status"] = @"1";
    parmDic[@"sku_id"] = self.userInfoModel.sku_id;
    parmDic[@"goods_owner_id"] = @"0";
    parmDic[@"goods_location_id"] = model.goods_location_id;
    parmDic[@"book_qty"] = model.available_qty;
    parmDic[@"counted_qty"] = @"0";
    parmDic[@"difference_qty"] = @"0";
    parmDic[@"spu_code"] = self.userInfoModel.spu_code;
    parmDic[@"spu_name"] = self.userInfoModel.spu_name;
    parmDic[@"sku_code"] = self.userInfoModel.sku_code;
    parmDic[@"warehouse_name"] = model.warehouse_name;
    parmDic[@"location_name"] = model.location_name;
    parmDic[@"adjust_status"] = @"0";
    
    [self showHudInView:self.view hint:@"正在加载数据..."];
    [MM_Http postRequestURLStr:KMM_Stocktaking withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        [self hideHud];
        
        //拿着id弹窗
        NSString *stockCount_id = [requestDic[@"data"] description];
        self.stockCount_id = stockCount_id;
        [self loadStockAlert:stockCount_id];
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        [self hideHud];
        [self showStateText:errorInfo];
    }];

}

//库存盘点第二步
- (void)stockAdd_Count_2Data:(NSMutableDictionary *)parmDic {
    
    [self showHudInView:self.view hint:@"正在加载数据..."];
    [MM_Http putRequestURLStr:KMM_Stocktaking withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        [self hideHud];
        
        [self loadStockListData];
        
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        [self hideHud];
        [self showStateText:errorInfo];
    }];

}

//库存盘点最后一步
- (void)stockAddStockCountData {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    parmDic[@"id"] = self.stockCount_id;
    
    [self showHudInView:self.view hint:@"正在加载数据..."];
    [MM_Http putRequestURLStr:KMM_Stocktaking withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        [self hideHud];
        
        [self showStateText:@"设置成功!"];
                
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        [self hideHud];
        [self showStateText:errorInfo];
    }];

}

- (void)loadStockListData {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    parmDic[@"id"] = self.stockCount_id;
    
    [self showHudInView:self.view hint:@"正在加载数据..."];
    [MM_Http getRequestURLStr:KMM_Stocktaking withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        [self hideHud];
        
        NSMutableArray *tempArr = [NSMutableArray array];
        
        MM_CodeResultTakeStockInfoModel *infoModel = [MM_CodeResultTakeStockInfoModel mj_objectWithKeyValues:requestDic];
        
        if (!stringIsEmpty(infoModel.book_qty)) {
            [tempArr addObject:infoModel.book_qty];
        } else {
            [tempArr addObject:@"0"];
        }
        
        if (!stringIsEmpty(infoModel.counted_qty)) {
            [tempArr addObject:infoModel.counted_qty];
        } else {
            [tempArr addObject:@"0"];
        }
        
        if (!stringIsEmpty(infoModel.difference_qty)) {
            [tempArr addObject:infoModel.difference_qty];
        } else {
            [tempArr addObject:@"0"];
        }
        self.resultAlertView.numberArr = tempArr;
        [self.resultAlertView show];

        MMLog(@"调用成功调用成功调用成功");
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        [self hideHud];
        [self showStateText:errorInfo];
    }];
}

//库存盘点第二步
- (void)stockCancle_Count_2Data:(NSString *)stockCount_id {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    parmDic[@"id"] = stockCount_id;
    
    [self showHudInView:self.view hint:@"正在加载数据..."];
    [MM_Http getRequestURLStr:KMM_Stocktaking withDic:parmDic success:^(NSDictionary * _Nonnull requestDic, NSString * _Nonnull msg) {
        [self hideHud];
                
        MMLog(@"调用成功调用成功调用成功");
    } failure:^(NSString * _Nonnull errorInfo, NSString * _Nonnull status) {
        
        [self hideHud];
        [self showStateText:errorInfo];
    }];

}

- (void)loadStockAlert:(NSString *)stockCount_id {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    
    WS(weakSelf);
    //提示框添加文本输入框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"库存盘点"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction *action) {
                                                         UITextField *textField = alert.textFields.firstObject;
        
        if (textField.text.integerValue > 0) {
            
            parmDic[@"counted_qty"] = textField.text;
            parmDic[@"id"] = stockCount_id;
            [weakSelf stockAdd_Count_2Data:parmDic];
            
        } else {
            [weakSelf showStateText:@"请添加盘点数量!"];
        }
                                                     }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction *action) {
        
        [weakSelf stockCancle_Count_2Data:stockCount_id];
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
//            textField.delegate = self;
        textField.textAlignment = NSTextAlignmentCenter;
        textField.keyboardType = UIKeyboardTypeNumberPad;
//            [textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    }];
    [okAction setValue:MM_ColorUtil(@"993300") forKey:@"titleTextColor"];
    [cancelAction setValue:MM_ColorUtil(@"#333333") forKey:@"titleTextColor"];
    [alert addAction:okAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];

}

- (UITableView *)tableView {
        
    if (_tableView == nil) {
        
        _tableView = [UITableView estimatedLayoutTableview:CGRectMake(0, NaviBarHeight, ScreenW, ScreenH - NaviBarHeight - HomeIndicatorHeight) tableviewStyle:UITableViewStyleGrouped delegateAndDataSource:self estimatedSectionHeader:YES estimatedSectionFooter:YES];
        _tableView.backgroundColor = MM_ColorUtil(@"#f7f7f7");
    }
    return _tableView;
}

- (void)oldCodeData {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    parmDic[@"id"] = self.userInfoModel.sku_id;
    parmDic[@"price_control"] = self.userInfoModel.price_control;

    WS(weakSelf);
    //提示框添加文本输入框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"输入编码"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction *action) {
        UITextField *textField = alert.textFields.firstObject;
        
        if (textField.text.integerValue > 0) {
            
            parmDic[@"old_code"] = textField.text;
            [weakSelf loadOldCodeData:parmDic];
        } else {
            [weakSelf showStateText:@"请输入编码!"];
        }
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction *action) {
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
//            textField.delegate = self;
        textField.textAlignment = NSTextAlignmentCenter;
        textField.keyboardType = UIKeyboardTypeNumberPad;
//            [textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    }];
    [okAction setValue:MM_ColorUtil(@"993300") forKey:@"titleTextColor"];
    [cancelAction setValue:MM_ColorUtil(@"#333333") forKey:@"titleTextColor"];
    [alert addAction:okAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
    
}

- (void)oldPriceData {
    
    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
    parmDic[@"id"] = self.userInfoModel.sku_id;
    parmDic[@"old_code"] = self.userInfoModel.old_code;
    
    WS(weakSelf);
    //提示框添加文本输入框
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"输入价格"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction *action) {
        UITextField *textField = alert.textFields.firstObject;
        
        if (textField.text.integerValue > 0) {
            
            parmDic[@"price_control"] = textField.text;
            [weakSelf loadOldCodeData:parmDic];
        } else {
            [weakSelf showStateText:@"请输入编码!"];
        }
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                         handler:^(UIAlertAction *action) {
    }];
    [alert addTextFieldWithConfigurationHandler:^(UITextField *textField) {
//            textField.delegate = self;
        textField.textAlignment = NSTextAlignmentCenter;
        textField.keyboardType = UIKeyboardTypeDecimalPad;
//            [textField addTarget:self action:@selector(textFieldChanged:) forControlEvents:UIControlEventEditingChanged];
    }];
    [okAction setValue:MM_ColorUtil(@"993300") forKey:@"titleTextColor"];
    [cancelAction setValue:MM_ColorUtil(@"#333333") forKey:@"titleTextColor"];
    [alert addAction:okAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
    
}

- (MM_CodeResultAlertView *)resultAlertView {
    
    if (_resultAlertView == nil) {
        
        _resultAlertView = [[MM_CodeResultAlertView alloc] init];
        WS(weakSelf);
        _resultAlertView.alertBlock = ^{
            
            [weakSelf stockAddStockCountData];
        };
    }
    return _resultAlertView;
}

- (MM_CodeResultIncreaseDecreaseAlertView *)incrDecrAlertView {
    
    if (_incrDecrAlertView == nil
        ) {
        
        _incrDecrAlertView = [[MM_CodeResultIncreaseDecreaseAlertView alloc] initWithFrame:CGRectZero];
        
        WS(weakSelf);
        _incrDecrAlertView.alertBlock = ^(NSInteger index, NSString * _Nonnull remark_str, NSString * _Nonnull number_str, NSString * _Nonnull dict_type, NSArray * _Nonnull imageArr) {
          
            if (index == 1) {
                
                if (imageArr.count > 0) {
                    
                    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
                    parmDic[@"change_qty"] = number_str;
                    parmDic[@"change_options"] = dict_type;
                    parmDic[@"reason"] = remark_str;
                    
                    [weakSelf startUploadPic:imageArr andDic:parmDic];
                    
                } else {
                    NSMutableDictionary *parmDic = [NSMutableDictionary dictionary];
                    parmDic[@"change_qty"] = number_str;
                    parmDic[@"change_options"] = dict_type;
                    parmDic[@"reason"] = remark_str;
                    parmDic[@"files"] = @[];

                    [weakSelf loadChangeNumberData:parmDic];
                }
                
            }
        };
    }
    return _incrDecrAlertView;
}

- (MM_CodeResultPicView *)picAlertView {
    
    if (_picAlertView == nil
        ) {
        
        _picAlertView = [[MM_CodeResultPicView alloc] initWithFrame:CGRectZero];
    }
    return _picAlertView;
}

- (void)startUploadPic:(NSArray *)imageArr andDic:(NSMutableDictionary *)parmDic {
    
    NSMutableDictionary *picDic = [NSMutableDictionary dictionary];
    
    [self showHudInView:self.view hint:@"正在加载数据..."];
    WS(weakSelf);
    [MM_Http MMPost:KMM_Sysfile_uploadFiles parameters:picDic constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        for (MM_UploadImageModel *model in imageArr) {
            
            if (model.tempFileData) {
                
                NSData *data = model.tempFileData;
                if (data) {
                    NSString * fileName = [weakSelf uploadFileName];
                    [formData appendPartWithFileData:data name:@"files" fileName:fileName mimeType:@"image/png"];
                }
            }
        }
        
    } progress:^(NSProgress * _Nonnull progress) {
                
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject) {
        
        //主线程刷新ui
        dispatch_async(dispatch_get_main_queue(), ^(){
            
            [self hideHud];
        });
        
        if (responseObject) {
            
            NSDictionary *requestDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            
            if ([requestDic isKindOfClass:[NSDictionary class]]) {
                id responeDic = requestDic[@"data"];
                if ([responeDic isKindOfClass:[NSArray class]]) {
                    
                    NSArray *picArr = (NSArray *)responeDic;
                    
                    if (picArr.count > 0) {
                        
                        NSMutableArray *tempArr = [NSMutableArray array];
                        
                        for (NSDictionary *picDic in picArr) {
                            
                            NSString *picStr = [picDic[@"url"] description];
                            
                            if (!stringIsEmpty(picStr)) {
                                
                                [tempArr addObject:picStr];
                            }

                        }
                        
                        parmDic[@"files"] = tempArr;
                        [weakSelf loadChangeNumberData:parmDic];
                        
                    }
                }
                
            }
        }
    } failure:^(NSURLSessionDataTask * _Nonnull operation, NSError * _Nonnull error) {
        [self hideHud];
    }];
}

/**
 *  获得唯一标示，当前的名字
 *
 *  @return
 */
- (NSString *)uploadFileName {
    
    NSDateFormatter* formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd_HH:mm:ss:SSS"];
    NSString *date = [formatter stringFromDate:[NSDate date]];
    return [date stringByAppendingString:@".png"];
    
}

- (NSMutableArray *)dataArr {
    
    if (_dataArr == nil) {
         
        _dataArr = [[NSMutableArray alloc] init];
    }
    return _dataArr;
}

- (NSMutableArray *)resonArr {
    
    if (_resonArr == nil) {
         
        _resonArr = [[NSMutableArray alloc] init];
    }
    return _resonArr;
}

- (void)setupRefresh {
    
    WS(weakSelf);
//    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        //请求数据
//        weakSelf.page_num = 1;
//        [weakSelf loadCodeData:weakSelf.code_str];
//    }];
    self.tableView.mj_footer = [MJRefreshAutoStateFooter footerWithRefreshingBlock:^{
        weakSelf.page_num += 1;
        [weakSelf loadOperationRecordMoreData];
    }];
}

/* 定义一个临时的变量暂存总的数据源 **/
- (void)updateTmpArr {
    
    NSMutableArray * tmp = [NSMutableArray array];
    for (MM_CodeResultSectionModel *sectionModel in self.dataArr) {
        
        if (sectionModel.sectionArr.count) {
            
            [tmp addObject:sectionModel];
        }
    }
    self.tempArr = tmp;
    
}
- (MM_CodeResultSectionModel *)sectionModelWithType:(MM_CodeResultSectionType)type {
    
    for (MM_CodeResultSectionModel *sectionModel in self.dataArr) {
        
        if (sectionModel.sectionType == type) {
            
            return sectionModel;
        }
    }
    return nil;
}

- (void)endRefersh {
    if (self.tableView.mj_header) {
        [self.tableView.mj_header endRefreshing];
    }
    if (self.tableView.mj_footer) {
        [self.tableView.mj_footer endRefreshing];
    }
}


@end
 
